<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <todoheader :receive="receive"/>
        <todomain :todoObj="todoObj"/>
        <todofooter :todoObj="todoObj" :allcheck="allcheck" :deledone="deledone" v-show="todoObj.length"/>
      </div>
    </div>
  </div>
</template>



<script>
  import todofooter from "./components/todofooter.vue"
  import todomain from "./components/todomain.vue"
  import todoheader from "./components/todoheader.vue"

  export default {
    name: 'App',
    data(){
      return{
        todoObj: JSON.parse(localStorage.getItem('todo')) || []
      }
    },
    components: {
      todofooter,todomain,todoheader
    },
    methods: {
      receive(e){
        this.todoObj.unshift(e)
      },
      checktodo(e){
        this.todoObj.forEach((index)=>{
          if(index.id===e){
            index.done = !index.done
          }
        })
      },
      detodo(id){
        this.todoObj = this.todoObj.filter(todedo=>todedo.id!==id)
      },
      allcheck(done){
        this.todoObj.forEach((e)=>{
          e.done=done
        })
      },
      deledone(){
        this.todoObj = this.todoObj.filter(tododo=>tododo.done!==true)
      },
      isEdit(todo,value){
        this.todoObj.forEach((index)=>{
          if(index.id===todo.id){
            index.text = value
          }
        })
      }
    },
    watch: {
      todoObj:{
        deep:true,
        handler(value){
          localStorage.setItem('todo',JSON.stringify(value))
        }
      }
    },
    mounted(){
      this.$bus.$on('checktodo',this.checktodo)
      this.$bus.$on('detodo',this.detodo)
      this.$bus.$on('isEdit',this.isEdit)
    },
    beforeDestroy(){
      this.$bus.$off('checktodo')
      this.$bus.$off('detodo')
      this.$bus.$off('isEdit')
    }
  }
</script>

<style>
  body {
    background: #fff;
  }

  .btn {
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    border-radius: 4px;
  }

  .btn-danger {
    color: #fff;
    background-color: #da4f49;
    border: 1px solid #bd362f;
  }
  .btn-change{
    color: #fff;
    background-color: skyblue;
    border: 1px solid rgb(98, 207, 250);
    margin-right: 6px;
  }

  .btn-danger:hover {
    color: #fff;
    background-color: #bd362f;
  }

  .btn:focus {
    outline: none;
  }
  .todo-container {
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
  }

</style>